1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 import java.awt.color.ColorSpace;
32 import java.awt.color.ICC_Profile;
33 import java.util.*;
34 import java.nio.*;
35 import java.util.Hashtable;
36
37 public class ReadWriteProfileTest implements Runnable {
38
39 final static int TAG_COUNT_OFFSET = 32;
40
41
42 final static int TAG_ELEM_OFFSET = 33;
43
44 static byte[][] profiles;
45 static int [][] tagSigs;
46 static Hashtable<Integer,byte[]> [] tags;
47
48 static int [] cspaces = {ColorSpace.CS_sRGB, ColorSpace.CS_PYCC,
49 ColorSpace.CS_LINEAR_RGB, ColorSpace.CS_CIEXYZ,
50 ColorSpace.CS_GRAY};
51
52 static String [] csNames = {"sRGB", "PYCC", "LINEAR_RGB", "CIEXYZ", "GRAY"};
53
54 static void getProfileTags(byte [] data, Hashtable tags) {
55 ByteBuffer byteBuf = ByteBuffer.wrap(data);
56 IntBuffer intBuf = byteBuf.asIntBuffer();
57 int tagCount = intBuf.get(TAG_COUNT_OFFSET);
58 intBuf.position(TAG_ELEM_OFFSET);
59 for (int i = 0; i < tagCount; i++) {
60 int tagSig = intBuf.get();
61 int tagDataOff = intBuf.get();
62 int tagSize = intBuf.get();
63
64 byte [] tagData = new byte[tagSize];
65 byteBuf.position(tagDataOff);
66 byteBuf.get(tagData);
67 tags.put(tagSig, tagData);
68 }
69 }
70
71 static {
72 profiles = new byte[cspaces.length][];
73 tags = new Hashtable[cspaces.length];
74
75 for (int i = 0; i < cspaces.length; i++) {
76 ICC_Profile pf = ICC_Profile.getInstance(cspaces[i]);
77 profiles[i] = pf.getData();
78 tags[i] = new Hashtable();
79 getProfileTags(profiles[i], tags[i]);
80 }
81 }
82
83 public void run() {
84 for (int i = 0; i < cspaces.length; i++) {
85 ICC_Profile pf = ICC_Profile.getInstance(cspaces[i]);
86 byte [] data = pf.getData();
87 pf = ICC_Profile.getInstance(data);
88 if (!Arrays.equals(data, profiles[i])) {
89 System.err.println("Incorrect result of getData() " + "with " +
90 csNames[i] + " profile");
91 throw new RuntimeException("Incorrect result of getData()");
92 }
93
94 for (int tagSig : tags[i].keySet()) {
95 byte [] tagData = pf.getData(tagSig);
96 byte [] empty = new byte[tagData.length];
97 boolean emptyDataRejected = false;
98 try {
99 pf.setData(tagSig, empty);
100 } catch (IllegalArgumentException e) {
101 emptyDataRejected = true;
102 }
103 if (!emptyDataRejected) {
104 throw new
105 RuntimeException("Test failed: empty tag data was not rejected.");
106 }
107 pf.setData(tagSig, tagData);
108
109 byte [] tagData1 = pf.getData(tagSig);
110
111 if (!Arrays.equals(tagData1, tags[i].get(tagSig)))
112 {
113 System.err.println("Incorrect result of getData(int) with" +
114 " tag " +
115 Integer.toHexString(tagSig) +
116 " of " + csNames[i] + " profile");
117
118 throw new RuntimeException("Incorrect result of " +
119 "getData(int)");
120 }
121 }
122 }
123 }
124
125 public static void main(String [] args) {
126 ReadWriteProfileTest test = new ReadWriteProfileTest();
127 test.run();
128 }
129 }